package role

import (
	"context"
	"time"

	"go-weixin/app/usercenter/api/internal/svc"
	"go-weixin/app/usercenter/api/internal/types"
	"go-weixin/app/usercenter/rpc/pb/proto/usercenter"

	"github.com/zeromicro/go-zero/core/logx"
)

type RoleDetailLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewRoleDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RoleDetailLogic {
	return &RoleDetailLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *RoleDetailLogic) RoleDetail(req *types.RoleDetailReq) (resp *types.RoleDetailResp, err error) {
	ctx, canelFunc := context.WithTimeout(context.Background(), 5*time.Second)
	defer canelFunc()
	res, err := l.svcCtx.UserRpc.RoleDetail(ctx, &usercenter.RoleDetailReq{
		Id: req.Id,
	})
	if err != nil {
		return nil, err
	}

	return &types.RoleDetailResp{
		RoleInfo: types.Role{
			Id:       res.RoleInfo.Id,
			Name:     res.RoleInfo.Name,
			Weight:   res.RoleInfo.Weight,
			ParentId: res.RoleInfo.ParentId,
			CreateAt: res.RoleInfo.CreateAt,
			UpdateAt: res.RoleInfo.UpdateAt,
		},
	}, nil
}
